/*    

    bobbietalk - a visual programming environment for toy robotics

    Copyright (C) 2013  Roberto Tiella

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.

*/
package bobbietalk.simulator.gui;

import bobbietalk.simulator.AbstractSimpleRobot;
import bobbietalk.simulator.RobotObserver;
import bobbietalk.simulator.world.World;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.geom.AffineTransform;
import java.awt.geom.GeneralPath;
import java.util.Observable;
import javax.swing.JPanel;

/**
 *
 * @author roberto
 */
public class WorldCanvas extends JPanel implements RobotObserver {

    private AbstractSimpleRobot robot;
    private World world = new World();

    public AbstractSimpleRobot getRobot() {
        return robot;
    }

    public void setRobot(AbstractSimpleRobot robot) {
        this.robot = robot;
        robot.addObserver(this);
        robot.setWorld(world);
    }

    @Override
    protected void paintComponent(Graphics g) {

        super.paintComponent(g);
        
        Graphics2D g2d = (Graphics2D) g;

        Rectangle b = getBounds();
        
        g2d.clearRect(b.x, b.y, b.width, b.height);

        AffineTransform t = AffineTransform.getTranslateInstance(b.width / 2, b.height / 2);
        g2d.setTransform(t);



        final double scale = 300;

        t.concatenate(AffineTransform.getScaleInstance(scale, scale));
        g2d.setTransform(t);

        g2d.setStroke(new BasicStroke((float) (1.0 / scale)));

        double rHeight = b.height / scale;
        double rWidth = b.width / scale;

        g2d.setColor(Color.lightGray);

        double gridSpace = 1; // meters

        for (double x = -3; x < 3; x += gridSpace) {

            GeneralPath p = new GeneralPath();
            p.moveTo(x, -rHeight / 2);
            p.lineTo(x, rHeight / 2);

            g2d.draw(p);

        }

        for (double y = -3; y < 3; y += gridSpace) {

            GeneralPath p = new GeneralPath();
            p.moveTo(-rWidth / 2, y);
            p.lineTo(rWidth / 2, y);

            g2d.draw(p);

        }

        if (robot != null) {


            g2d.setColor(Color.blue);



            g2d.draw(robot.getShape());
            g2d.draw(world.getWall().getShape());

        }


    }

    @Override
    public void update(Observable o, Object arg) {

        repaint();

    }
}
